package com.dstation.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.domain.AdministratorInfo;
import com.dstation.domain.ClassificationInfo;
import com.dstation.domain.TagInfo;
import com.dstation.service.interf.ITagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Objects;

@Slf4j
@Controller
@RequestMapping(value = "/tag")
public class TagController {
    final int pageSize=10;

    @Autowired
    private ITagService tagService;

    @RequestMapping(value = "viewTag")
    public String viewTag(@PathParam("page")Integer page, @PathParam("search")String search, ModelMap modelMap){
        if(Objects.isNull(page)){
            return "";
        }
        IPage<TagInfo> pageData=tagService.findShowTags(search,new Page(page,pageSize));

        modelMap.put("pageData",pageData);

        return "admin/label";
    }



    @ResponseBody
    @RequestMapping(value = "save")
    public JSONObject saveTag(@PathParam("taginfo")TagInfo tagInfo){
        JSONObject jsonObject=new JSONObject();


        boolean isRepeat=isTagNameRepeat(tagInfo.getTagName());
        boolean isSave=false;

        if(!isRepeat){
            isSave=tagService.addTag(tagInfo);
        }

        jsonObject.put("isSave",isSave);

        return jsonObject;
    }

    @ResponseBody
    @RequestMapping(value = "update")
    public JSONObject modifyTag(@PathParam("oldName") String oldName,@PathParam("newName")String newName){
        JSONObject jsonObject=new JSONObject();

//        AdministratorInfo administratorInfo=(AdministratorInfo) httpSession.getAttribute("userInfo");

        boolean isRepeat=isTagNameRepeat(newName);
        boolean isModify=false;

        if(!isRepeat){
            long id=tagService.findTagIdByTagName(oldName);


            isModify=tagService.alterTag(new TagInfo(id,newName));
        }

        jsonObject.put("isModify",isModify);

        return jsonObject;
    }


    @ResponseBody
    @RequestMapping(value = "delete")
    public JSONObject removeTag( @PathParam("tagId")long tagId){
        JSONObject jsonObject=new JSONObject();



        boolean isRemove=false;

        isRemove=tagService.dropTag(tagId);


        jsonObject.put("isDelete",isRemove);

        return jsonObject;
    }

    private boolean isTagNameRepeat(String tagName) {
        return tagService.queryIsTagNameRepeat(tagName);
    }


}
